<template>
  <el-container style="height: 100vh;">
    <!-- 左侧菜单 -->
    <el-aside width="200px" style="background-color: #f4f4f4;">
      <el-menu :default-active="activeMenu" @select="handleMenuSelect" background-color="#ffffff" text-color="#000000" active-text-color="#409EFF">
        <el-menu-item index="1">数据统计</el-menu-item>
        <el-menu-item index="2">菜品管理</el-menu-item>
        <el-menu-item index="3">分类管理</el-menu-item>
        <el-menu-item index="4">订单管理</el-menu-item>
        <el-menu-item index="5">桌号管理</el-menu-item>
      </el-menu>
    </el-aside>

    <!-- 右侧内容 -->
    <el-container>
      <!-- 顶部 -->
      <el-header style="background-color: #fff; padding: 10px 20px;">
        <el-row type="flex" justify="space-between" align="middle">
          <el-col>
            <span>欢迎, {{ username }}</span>
          </el-col>
          <el-col>
            <el-button type="text" size="small" @click="logout">退出登录</el-button>
          </el-col>
        </el-row>
      </el-header>

      <!-- 主要内容区域 -->
      <el-main>
        <div v-if="activeMenu === '1'">
          <h2>数据统计页面</h2>
        </div>
        <div v-if="activeMenu === '2'">
          <DishManage />
        </div>
        <div v-if="activeMenu === '3'">
          <CategoryManage />
        </div>
        <div v-if="activeMenu === '4'">
          <OrderManage />
        </div>
        <div v-if="activeMenu === '5'">
          <TableManage />
        </div>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import OrderManage from './OrderManage.vue'
import TableManage from './TableManage.vue'
import DishManage from './DishManage.vue'
import CategoryManage from './CategoryManage.vue'
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { ElMenu, ElMenuItem, ElContainer, ElAside, ElHeader, ElMain, ElButton, ElRow, ElCol } from 'element-plus';

export default {
  name: 'Home',
  components: {
    ElMenu,
    ElMenuItem,
    ElContainer,
    ElAside,
    ElHeader,
    ElMain,
    ElButton,
    ElRow,
    ElCol,
    CategoryManage,
    DishManage,
    TableManage,
    OrderManage
  },
  setup() {
    const activeMenu = ref('1');
    // 从localStorage读取用户名，默认显示"用户名称"
    const username = ref(localStorage.getItem('username') || '用户名称');
    const router = useRouter();

    const handleMenuSelect = (index) => {
      activeMenu.value = index;
    };

    // 退出登录：清除本地存储并跳转登录页
    const logout = () => {
      localStorage.removeItem('token');
      localStorage.removeItem('username');
      router.push('/login');
    };

    return {
      activeMenu,
      username,
      handleMenuSelect,
      logout
    };
  }
};
</script>

<style scoped>
.el-aside {
  background-color: #f4f4f4;
}
</style>